package nuc.wxkj.boot_zb02.controller;

import nuc.wxkj.boot_zb02.dao.DeptDao;
import nuc.wxkj.boot_zb02.dao.EmpMapper;
import nuc.wxkj.boot_zb02.entity.Dept;
import nuc.wxkj.boot_zb02.entity.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class DeptController {

    @Autowired
    DeptDao deptDao;

    @Autowired
    EmpMapper empMapper;

    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @GetMapping("/welcome")
    String welcome(){
        return "welcome";
    }

    @GetMapping("/deptAll")
    String a (Model model){
        model.addAttribute("depts",deptDao.selectDepts());
        System.out.println(deptDao.selectDepts());
        return "department_list";
    }

    @GetMapping("/toUpdateDept")
    String toUpdateDept(@RequestParam int id,Model model){
        Dept dept = deptDao.selectDeptById(id);
        System.out.println(dept);
        model.addAttribute("dept",dept);
        return "department_update";
    }
//    return "redirect:/deptAll";
    @PostMapping("/doUpdateDept")
    public String doUpdateDept(Dept dept){
        System.out.println(dept);
        int i = deptDao.updateDept(dept);
        return "redirect:/deptAll";
    }

    @GetMapping("/toAddDeptment")
    public String toAddDeptment(){
        return "department_add";

    }

    @PostMapping("/doAddDeptment")
    public String doAddDeptment(Dept dept){
        System.out.println(dept);
        deptDao.addDept(dept);
        return "redirect:/deptAll";
    }

    @GetMapping("/toDeteleDept")
    public String delDpet(@RequestParam String id){
 /*       int i = deptDao.deleteDept(id);
        System.out.println("执行结果"+i);*/
        Integer id1 = Integer.parseInt(id);
        System.out.println(id);
        return "redirect:/deptAll";
    }

    @GetMapping("/to")
    public String string(Model model){
        final List<Dept> depts = empMapper.selectDept_emp();
        model.addAttribute("depts",depts);
        final List<Emp> emps = empMapper.selectEmp_DP();
        model.addAttribute("emps",emps);
        return "/employee_list";
    }

    @GetMapping("demo")
    String demo(){
        return "demo";
    }
}
